#!/bin/bash

if [ $# != 2 ] && [ $# != 1 ]; then
	echo "导入异常：参数错误"
	exit 0
fi

config_dir="/usr/local/pemd/data"
if [ $# -eq 2 ];then
	config_dir=$2
fi
old_config_file="${config_dir}/protocol_rule_old.db"
config_file="${config_dir}/protocol_rule.db"

echo "开始更新配置文件:$1"

if [ -n "$1" -a -f "$1" ]; then
	echo "停止pemd进程..."
	if [ -f ${old_config_file} ]; then
		echo "删除备份的配置文件..."
		rm ${old_config_file}
	fi
	echo "停止pemd进程..."
	systemctl stop pemd
	if [ -f ${config_file} ]; then
		echo "备份当前的配置文件..."
		mv ${config_file} ${old_config_file}
	fi
	echo "更新配置文件..."
	cp -f $1 ${config_file}
	echo "启动pemd进程..."
	systemctl start pemd
	ps -fe|grep processString |grep -v grep
    isExists=$?
	if [ ${isExists} -ne 0 ]; then
		echo "启动pemd进程成功"
	else
		echo "启动pemd进程失败"
	fi
else
	echo "$1文件不存在"
fi
